package com.intramirror.wd.business.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

/**
 * Created by niaoge on 2016/12/17.
 */
public class CopyUtil {
    public static <T> T copy(Object source,Class<T> targetCls, String... ignoreProperties) {
        if(source == null){
            return null;
        }
        T targetObj = BeanUtils.instantiate(targetCls);
        BeanUtils.copyProperties(source,targetObj,ignoreProperties);
        return targetObj;
    }

    public static <T> List<T> copy(List<?> sourceList, Class<T> targetCls, String... ignoreProperties) {
        if(sourceList == null){
            return null;
        }
        
        if(CollectionUtil.isNotEmpty(sourceList)){
            List<T> targetList = new ArrayList<T>(sourceList.size());
            for(Object o:sourceList){
                T targetObj = BeanUtils.instantiate(targetCls);
                BeanUtils.copyProperties(o,targetObj,ignoreProperties);
                targetList.add(targetObj);
            }
            return targetList;
        }
        return new ArrayList<T>(0);
    }

}
